#include "insertlogin.h"
#include "ui_insertlogin.h"
#include <QMessageBox>
#include "mainwindow.h"
#include "globle.h"
#include <QtDebug>
#include "userlogin.h"

insertlogin::insertlogin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::insertlogin)
{
    ui->setupUi(this);
}

insertlogin::~insertlogin()
{
    delete ui;
}

void insertlogin::on_insertloginButton_clicked()
{
    QString name=ui->editName->text();
    QString pswd=ui->editPswd->text();
    QString rePswd=ui->editRePswd->text();

    if(name.isEmpty()||pswd.isEmpty()||rePswd.isEmpty()){
        QMessageBox::warning(this ,tr("警告") , tr("请输入完整的信息."));
        return;
    }

    if(pswd!=rePswd){
        QMessageBox::warning(this ,tr("警告") , tr("两次密码输入不一致."));
        return;
    }

    QSqlDatabase db;

    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("studentmanager");
    db.setUserName(sqluser);
    db.setPassword(sqlpass);
    db.open();

    QSqlQuery query(db);

    if(ui->radioAdmin->isChecked()){
        query.exec(QString("select * from admin;"));
        int maxIndex=0;
        query.first();
        for(;query.isValid();query.next()){
            if(query.value("username").toString()==name){
                QMessageBox::warning(this ,tr("警告") , tr("该账号已存在."));
                db.close();
                return;
            }
            if(query.value("id").toInt()>maxIndex)maxIndex=query.value("id").toInt();
        }

        if(query.exec(QString("insert into admin(id,username,password) values(%1,'%2','%3');").arg(maxIndex+1).arg(name).arg(pswd))){
            QMessageBox::information(this ,tr("提示") , tr("添加用户成功!"));

            db.close();
            return;
        }else{
            QMessageBox::warning(this ,tr("警告") , tr("添加用户失败.."));
            db.close();
            return;
        }


    }else if(ui->radioTeacher->isChecked()){
        query.exec(QString("select * from teacher;"));
        int maxIndex=0;
        query.first();
        for(;query.isValid();query.next()){
            if(query.value("name").toString()==name){
                QMessageBox::warning(this ,tr("警告") , tr("该账号已存在."));
                db.close();
                return;
            }
            if(query.value("id").toInt()>maxIndex)maxIndex=query.value("id").toInt();
        }

        if(query.exec(QString("insert into teacher(id,name,pswd) values(%1,'%2','%3');").arg(maxIndex+1).arg(name).arg(pswd))){
            QMessageBox::information(this ,tr("提示") , tr("添加用户成功!"));

            db.close();
            return;
        }else{
            QMessageBox::warning(this ,tr("警告") , tr("添加用户失败.."));
            db.close();
            return;
        }
    }else if(ui->radioStudent->isChecked()){
        query.exec(QString("select * from student;"));
        int maxIndex=0;
        query.first();
        for(;query.isValid();query.next()){
            if(query.value("name").toString()==name){
                QMessageBox::warning(this ,tr("警告") , tr("该账号已存在."));
                db.close();
                return;
            }
            if(query.value("id").toInt()>maxIndex)maxIndex=query.value("id").toInt();
        }

        if(query.exec(QString("insert into student(id,name,password) values(%1,'%2','%3');").arg(maxIndex+1).arg(name).arg(pswd))){
            QMessageBox::information(this ,tr("提示") , tr("添加用户成功!"));

            db.close();
            return;
        }else{
            QMessageBox::warning(this ,tr("警告") , tr("添加用户失败.."));
            db.close();
            return;
        }
    }
}



void insertlogin::on_insert_back_Button_clicked()
{
    this->hide();
    userlogin *qu=new userlogin;
    qu->show();
}
